<?php
namespace App\Utility;
use EasySwoole\HttpClient\HttpClient;

class TxWebService
{

    private static $instance = null;
    private $key;
    private $url = "https://apis.map.qq.com/ws/location/v1/ip";

    private function __construct($key)
    {
        $this->key = $key;

    }

    public static function getInstance($key = "")
    {
        if(self::$instance === null){
            self::$instance = new self($key);
        }
        return self::$instance;
    }

    /**
     * ip转省市区
     */
    public function ipToAddress($ip){

        $http = new HttpClient($this->url.'?key='.$this->key.'&ip='.$ip);
        $res = $http->get();

        $data = json_decode($res->getBody(),true);
//        var_dump($data);
        if($data['status'] != 0){

            return false;
        }



        return  $data['result']['ad_info'];

    }
}

?>